package com.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.entity.MailUser;
import com.service.UserService;
import com.utils.UUIDUtils;

@Controller
public class UserController {
	
	@Autowired
	private UserService userService;
	

	@RequestMapping("/")
	public String first() {
		return "regist";
	}
	
	@RequestMapping("/ActiveCount")
	@ResponseBody
	public Map<String, Object> ActiveCount(HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		String code = request.getParameter("code");
		try {
			userService.update(code);
			result.put("msg", "激活成功");
		} catch (Exception e) {
			result.put("msg", "激活失败");
			e.printStackTrace();
		}
		return result;
	}
	
	@RequestMapping("/registUser")
	@ResponseBody
	public Map<String, Object> registUser(HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String nickname = request.getParameter("nickname");
		String email = request.getParameter("email");
		MailUser entity = new MailUser();
		entity.setUsername(username);
		entity.setPassword(password);
		entity.setNickname(nickname);
		entity.setEmail(email);
		entity.setState(0); //0:未激活 ；1：已激活
		entity.setCode(UUIDUtils.getUUID()); //激活码
		
		try {
			userService.insert(entity);
			result.put("msg", "注册成功");
		} catch (Exception e) {
			result.put("msg", "注册失败");
			e.printStackTrace();
		}
		return result;
	}
}
